home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / POV-Ray 3.0.2 / src / SOURCE / LIBPNG / PNGRUTIL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-16  |  50.3 KB  |  1,822 lines  |  [TEXT/CWIE]

  1.  
  2. /* pngrutil.c - utilities to read a png file
  3.  
  4.    libpng 1.0 beta 4 - version 0.90
  5.    For conditions of distribution and use, see copyright notice in png.h
  6.    Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  7.    January 10, 1997
  8.    */
  9.  
  10. #define PNG_INTERNAL
  11. #include "png.h"
  12.  
  13. /* grab an uint 32 from a buffer */
  14. png_uint_32
  15. png_get_uint_32(png_bytep buf)
  16. {
  17.    png_uint_32 i;
  18.  
  19.    i = ((png_uint_32)(*buf) << 24) +
  20.       ((png_uint_32)(*(buf + 1)) << 16) +
  21.       ((png_uint_32)(*(buf + 2)) << 8) +
  22.       (png_uint_32)(*(buf + 3));
  23.  
  24.    return i;
  25. }
  26.  
  27. /* grab an uint 16 from a buffer */
  28. png_uint_16
  29. png_get_uint_16(png_bytep buf)
  30. {
  31.    png_uint_16 i;
  32.  
  33.    i = (png_uint_16)(((png_uint_16)(*buf) << 8) +
  34.       (png_uint_16)(*(buf + 1)));
  35.  
  36.    return i;
  37. }
  38.  
  39. /* Set the action on getting a CRC error for an ancillary or critical chunk. */
  40. void
  41. png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action)
  42. {
  43.    /* Tell libpng how we react to CRC errors in critical chunks */
  44.    switch (crit_action)
  45.    {
  46.       case PNG_CRC_NO_CHANGE:                        /* leave setting as is */
  47.          break;
  48.       case PNG_CRC_WARN_USE:                               /* warn/use data */
  49.          png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
  50.          png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE;
  51.          break;
  52.       case PNG_CRC_QUIET_USE:                             /* quiet/use data */
  53.          png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
  54.          png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE |
  55.                            PNG_FLAG_CRC_CRITICAL_IGNORE;
  56.          break;
  57.       case PNG_CRC_WARN_DISCARD:    /* not a valid action for critical data */
  58.          png_warning(png_ptr, "Can't discard critical data on CRC error.");
  59.       case PNG_CRC_ERROR_QUIT:                                /* error/quit */
  60.       case PNG_CRC_DEFAULT:
  61.       default:
  62.          png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
  63.          break;
  64.    }
  65.  
  66.    switch (ancil_action)
  67.    {
  68.       case PNG_CRC_NO_CHANGE:                       /* leave setting as is */
  69.          break;
  70.       case PNG_CRC_WARN_USE:                              /* warn/use data */
  71.          png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
  72.          png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE;
  73.          break;
  74.       case PNG_CRC_QUIET_USE:                            /* quiet/use data */
  75.          png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
  76.          png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE |
  77.                            PNG_FLAG_CRC_ANCILLARY_NOWARN;
  78.          break;
  79.       case PNG_CRC_ERROR_QUIT:                               /* error/quit */
  80.          png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
  81.          png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN;
  82.          break;
  83.       case PNG_CRC_WARN_DISCARD:                      /* warn/discard data */
  84.       case PNG_CRC_DEFAULT:
  85.       default:
  86.          png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
  87.          break;
  88.    }
  89. }
  90.  
  91. /* Read data, and (optionally) run it through the CRC. */
  92. void
  93. png_crc_read(png_structp png_ptr, png_bytep buf, png_uint_32 length)
  94. {
  95.    int need_crc = 1;
  96.  
  97.    if (png_ptr->chunk_name[0] & 0x20)                    /* ancillary */
  98.    {
  99.       if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
  100.           (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
  101.          need_crc = 0;
  102.    }
  103.    else                                                   /* critical */
  104.    {
  105.       if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
  106.          need_crc = 0;
  107.    }
  108.  
  109.    png_read_data(png_ptr, buf, length);
  110.  
  111.    if (need_crc)
  112.       png_calculate_crc(png_ptr, buf, length);
  113. }
  114.  
  115. /* Optionally skip data and then check the CRC.  Depending on whether we
  116.    are reading a ancillary or critical chunk, and how the program has set
  117.    things up, we may calculate the CRC on the data and print a message. */
  118. int
  119. png_crc_finish(png_structp png_ptr, png_uint_32 skip)
  120. {
  121.    int need_crc = 1;
  122.    int crc_error;
  123.    png_uint_32 i;
  124.  
  125.    if (png_ptr->chunk_name[0] & 0x20)                     /* ancillary */
  126.    {
  127.       if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
  128.           (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
  129.          need_crc = 0;
  130.    }
  131.    else                                                    /* critical */
  132.    {
  133.       if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
  134.          need_crc = 0;
  135.    }
  136.  
  137.    for (i = skip; i > png_ptr->zbuf_size; i -= png_ptr->zbuf_size)
  138.    {
  139.       png_read_data(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
  140.       if (need_crc)
  141.          png_calculate_crc(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
  142.    }
  143.    if (i)
  144.    {
  145.       png_read_data(png_ptr, png_ptr->zbuf, i);
  146.       if (need_crc)
  147.          png_calculate_crc(png_ptr, png_ptr->zbuf, i);
  148.    }
  149.  
  150.    crc_error = png_crc_error(png_ptr);
  151.  
  152.    if (need_crc && crc_error)
  153.    {
  154.       char msg[80];
  155.  
  156.       sprintf(msg,"CRC error in %s", png_ptr->chunk_name);
  157.  
  158.       if ((png_ptr->chunk_name[0] & 0x20 &&                /* Ancillary */
  159.            !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) ||
  160.           (!(png_ptr->chunk_name[0] & 0x20) &&             /* Critical  */
  161.            png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE))
  162.       {
  163.          png_warning(png_ptr, msg);
  164.       }
  165.       else
  166.       {
  167.          png_error(png_ptr, msg);
  168.       }
  169.       return 1;
  170.    }
  171.  
  172.    return 0;
  173. }
  174.  
  175. /* Compare the CRC stored in the PNG file with that calulated by libpng from
  176.    the data it has read thus far. */
  177. int
  178. png_crc_error(png_structp png_ptr)
  179. {
  180.    png_byte crc_bytes[4];
  181.    png_uint_32 crc;
  182.  
  183.    png_read_data(png_ptr, crc_bytes, 4);
  184.  
  185.    crc = png_get_uint_32(crc_bytes);
  186.  
  187. #ifdef PNG_USE_OWN_CRC
  188.    return (((crc^0xffffffffL) & 0xffffffffL) != (png_ptr->crc & 0xffffffffL));
  189. #else
  190.    return (crc != png_ptr->crc);
  191. #endif
  192. }
  193.  
  194.  
  195. /* read and check the IDHR chunk */
  196. void
  197. png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  198. {
  199.    png_byte buf[13];
  200.    png_uint_32 width, height;
  201.    int bit_depth, color_type, compression_type, filter_type;
  202.    int interlace_type;
  203.  
  204.    if (png_ptr->mode != PNG_BEFORE_IHDR)
  205.       png_error(png_ptr, "Out of place IHDR");
  206.  
  207.    /* check the length */
  208.    if (length != 13)
  209.       png_error(png_ptr, "Invalid IHDR chunk");
  210.  
  211.    png_ptr->mode |= PNG_HAVE_IHDR;
  212.  
  213.    png_crc_read(png_ptr, buf, 13);
  214.    png_crc_finish(png_ptr, 0);
  215.  
  216.    width = png_get_uint_32(buf);
  217.    height = png_get_uint_32(buf + 4);
  218.    bit_depth = buf[8];
  219.    color_type = buf[9];
  220.    compression_type = buf[10];
  221.    filter_type = buf[11];
  222.    interlace_type = buf[12];
  223.  
  224.    /* check for width and height valid values */
  225.    if (width == 0 || width > 2147483647 || height == 0 || height > 2147483647)
  226.       png_error(png_ptr, "Invalid image size in IHDR");
  227.  
  228.    /* check other values */
  229.    if (bit_depth != 1 && bit_depth != 2 &&
  230.       bit_depth != 4 && bit_depth != 8 &&
  231.       bit_depth != 16)
  232.       png_error(png_ptr, "Invalid bit depth in IHDR");
  233.  
  234.    if (color_type < 0 || color_type == 1 ||
  235.       color_type == 5 || color_type > 6)
  236.       png_error(png_ptr, "Invalid color type in IHDR");
  237.  
  238.    if ((color_type == PNG_COLOR_TYPE_PALETTE && bit_depth) > 8 ||
  239.        ((color_type == PNG_COLOR_TYPE_RGB ||
  240.          color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
  241.          color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
  242.       png_error(png_ptr, "Invalid color type/bit depth combination in IHDR");
  243.  
  244.    if (interlace_type > 1)
  245.       png_error(png_ptr, "Unknown interlace method in IHDR");
  246.  
  247.    if (compression_type > 0)
  248.       png_error(png_ptr, "Unknown compression method in IHDR");
  249.  
  250.    if (filter_type > 0)
  251.       png_error(png_ptr, "Unknown filter method in IHDR");
  252.  
  253.    /* set internal variables */
  254.    png_ptr->width = width;
  255.    png_ptr->height = height;
  256.    png_ptr->bit_depth = (png_byte)bit_depth;
  257.    png_ptr->interlaced = (png_byte)interlace_type;
  258.    png_ptr->color_type = (png_byte)color_type;
  259.  
  260.    /* find number of channels */
  261.    switch (png_ptr->color_type)
  262.    {
  263.       case 0:
  264.       case 3:
  265.          png_ptr->channels = 1;
  266.          break;
  267.       case 2:
  268.          png_ptr->channels = 3;
  269.          break;
  270.       case 4:
  271.          png_ptr->channels = 2;
  272.          break;
  273.       case 6:
  274.          png_ptr->channels = 4;
  275.          break;
  276.    }
  277.  
  278.    /* set up other useful info */
  279.    png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
  280.       png_ptr->channels);
  281.    png_ptr->rowbytes = ((png_ptr->width *
  282.       (png_uint_32)png_ptr->pixel_depth + 7) >> 3);
  283.    png_read_IHDR(png_ptr, info_ptr, width, height, bit_depth,
  284.       color_type, compression_type, filter_type, interlace_type);
  285. }
  286.  
  287. /* read and check the palette */
  288. void
  289. png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  290. {
  291.    png_colorp palette;
  292.    int num, i;
  293.  
  294.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  295.       png_error(png_ptr, "Missing IHDR before PLTE");
  296.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  297.    {
  298.       png_warning(png_ptr, "Invalid PLTE after IDAT");
  299.       png_crc_finish(png_ptr, length);
  300.       return;
  301.    }
  302.    else if (png_ptr->mode & PNG_HAVE_PLTE)
  303.       png_error(png_ptr, "Duplicate PLTE chunk");
  304.  
  305.    png_ptr->mode |= PNG_HAVE_PLTE;
  306.  
  307. #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
  308.    if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  309.    {
  310.       png_crc_finish(png_ptr, length);
  311.       return;
  312.    }
  313. #endif
  314.  
  315.    if (length % 3)
  316.    {
  317.       if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  318.       {
  319.          png_warning(png_ptr, "Invalid palette chunk");
  320.          png_crc_finish(png_ptr, length);
  321.          return;
  322.       }
  323.       else
  324.       {
  325.          png_error(png_ptr, "Invalid palette chunk");
  326.       }
  327.    }
  328.  
  329.    num = (int)length / 3;
  330.    palette = (png_colorp)png_malloc(png_ptr, num * sizeof (png_color));
  331.    png_ptr->flags |= PNG_FLAG_FREE_PALETTE;
  332.    for (i = 0; i < num; i++)
  333.    {
  334.       png_byte buf[3];
  335.  
  336.       png_crc_read(png_ptr, buf, 3);
  337.       /* don't depend upon png_color being any order */
  338.       palette[i].red = buf[0];
  339.       palette[i].green = buf[1];
  340.       palette[i].blue = buf[2];
  341.    }
  342.  
  343.    /* If we actually NEED the PLTE chunk (ie for a paletted image), we do
  344.       whatever the normal CRC configuration tells us.  However, if we
  345.       have an RGB image, the PLTE can be considered ancillary, so
  346.       we will act as though it is. */
  347.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  348.    {
  349.       if (png_crc_finish(png_ptr, 0))
  350.          return;
  351.    }
  352.    else if (png_crc_error(png_ptr))  /* Only if we have a CRC error */
  353.    {
  354.       char msg[80];
  355.  
  356.       sprintf(msg,"CRC error in %s", png_ptr->chunk_name);
  357.  
  358.       /* If we don't want to use the data from an ancillary chunk,
  359.          we have two options: an error abort, or a warning and we
  360.          ignore the data in this chunk (which should be OK, since
  361.          it's considered ancillary for a RGB or RGBA image). */
  362.       if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
  363.       {
  364.          if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
  365.          {
  366.             png_error(png_ptr, msg);
  367.          }
  368.          else
  369.          {
  370.             png_warning(png_ptr, msg);
  371.             return;
  372.          }
  373.       }
  374.       else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
  375.       {
  376.          png_warning(png_ptr, msg);
  377.       }
  378.    }
  379.  
  380.    png_ptr->palette = palette;
  381.    png_ptr->num_palette = (png_uint_16)num;
  382.    png_read_PLTE(png_ptr, info_ptr, palette, num);
  383. }
  384.  
  385. void
  386. png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  387. {
  388.    if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
  389.    {
  390.       png_error(png_ptr, "No image in file");
  391.    }
  392.  
  393.    png_ptr->mode |= PNG_AFTER_IDAT | PNG_HAVE_IEND;
  394.  
  395.    if (length != 0)
  396.    {
  397.       png_warning(png_ptr, "Incorrect IEND chunk length");
  398.       png_crc_finish(png_ptr, length);
  399.    }
  400. }
  401.  
  402. #if defined(PNG_READ_gAMA_SUPPORTED)
  403. void
  404. png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  405. {
  406.    png_uint_32 igamma;
  407.    float gamma;
  408.    png_byte buf[4];
  409.  
  410.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  411.       png_error(png_ptr, "Missing IHDR before gAMA");
  412.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  413.    {
  414.       png_warning(png_ptr, "Invalid gAMA after IDAT");
  415.       png_crc_finish(png_ptr, length);
  416.       return;
  417.    }
  418.    else if (png_ptr->mode & PNG_HAVE_PLTE)
  419.       /* Should be an error, but we can cope with it */
  420.       png_warning(png_ptr, "Out of place gAMA chunk");
  421.    else if (info_ptr && info_ptr->valid & PNG_INFO_gAMA)
  422.    {
  423.       png_warning(png_ptr, "Duplicate gAMA chunk");
  424.       png_crc_finish(png_ptr, length);
  425.       return;
  426.    }
  427.  
  428.    if (length != 4)
  429.    {
  430.       png_warning(png_ptr, "Incorrect gAMA chunk length");
  431.       png_crc_finish(png_ptr, length);
  432.       return;
  433.    }
  434.  
  435.    png_crc_read(png_ptr, buf, 4);
  436.    if (png_crc_finish(png_ptr, 0))
  437.       return;
  438.  
  439.    igamma = png_get_uint_32(buf);
  440.    /* check for zero gamma */
  441.    if (!igamma)
  442.       return;
  443.  
  444.    gamma = (float)igamma / (float)100000.0;
  445.    png_ptr->gamma = gamma;
  446.    png_read_gAMA(png_ptr, info_ptr, gamma);
  447. }
  448. #endif
  449.  
  450. #if defined(PNG_READ_sBIT_SUPPORTED)
  451. void
  452. png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  453. {
  454.    png_uint_32 truelen;
  455.    png_byte buf[4];
  456.  
  457.    buf[0] = buf[1] = buf[2] = buf[3] = 0;
  458.  
  459.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  460.       png_error(png_ptr, "Missing IHDR before sBIT");
  461.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  462.    {
  463.       png_warning(png_ptr, "Invalid sBIT after IDAT");
  464.       png_crc_finish(png_ptr, length);
  465.       return;
  466.    }
  467.    else if (png_ptr->mode & PNG_HAVE_PLTE)
  468.       /* Should be an error, but we can cope with it */
  469.       png_warning(png_ptr, "Out of place sBIT chunk");
  470.    else if (info_ptr && info_ptr->valid & PNG_INFO_sBIT)
  471.    {
  472.       png_warning(png_ptr, "Duplicate sBIT chunk");
  473.       png_crc_finish(png_ptr, length);
  474.       return;
  475.    }
  476.  
  477.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  478.       truelen = 3;
  479.    else
  480.       truelen = png_ptr->channels;
  481.  
  482.    if (length != truelen)
  483.    {
  484.       png_warning(png_ptr, "Incorrect sBIT chunk length");
  485.       png_crc_finish(png_ptr, length);
  486.       return;
  487.    }
  488.  
  489.    png_crc_read(png_ptr, buf, length);
  490.    if (png_crc_finish(png_ptr, 0))
  491.       return;
  492.  
  493.    if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  494.    {
  495.       png_ptr->sig_bit.red = buf[0];
  496.       png_ptr->sig_bit.green = buf[1];
  497.       png_ptr->sig_bit.blue = buf[2];
  498.       png_ptr->sig_bit.alpha = buf[3];
  499.    }
  500.    else
  501.    {
  502.       png_ptr->sig_bit.gray = buf[0];
  503.       png_ptr->sig_bit.alpha = buf[1];
  504.    }
  505.    png_read_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
  506. }
  507. #endif
  508.  
  509. #if defined(PNG_READ_cHRM_SUPPORTED)
  510. void
  511. png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  512. {
  513.    png_byte buf[4];
  514.    png_uint_32 val;
  515.    float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
  516.  
  517.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  518.       png_error(png_ptr, "Missing IHDR before sBIT");
  519.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  520.    {
  521.       png_warning(png_ptr, "Invalid cHRM after IDAT");
  522.       png_crc_finish(png_ptr, length);
  523.       return;
  524.    }
  525.    else if (png_ptr->mode & PNG_HAVE_PLTE)
  526.       /* Should be an error, but we can cope with it */
  527.       png_warning(png_ptr, "Missing PLTE before cHRM");
  528.    else if (info_ptr && info_ptr->valid & PNG_INFO_cHRM)
  529.    {
  530.       png_warning(png_ptr, "Duplicate cHRM chunk");
  531.       png_crc_finish(png_ptr, length);
  532.       return;
  533.    }
  534.  
  535.    if (length != 32)
  536.    {
  537.       png_warning(png_ptr, "Incorrect cHRM chunk length");
  538.       png_crc_finish(png_ptr, length);
  539.       return;
  540.    }
  541.  
  542.    png_crc_read(png_ptr, buf, 4);
  543.    val = png_get_uint_32(buf);
  544.    white_x = (float)val / (float)100000.0;
  545.  
  546.    png_crc_read(png_ptr, buf, 4);
  547.    val = png_get_uint_32(buf);
  548.    white_y = (float)val / (float)100000.0;
  549.  
  550.    if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
  551.        white_x + white_y > 1.0)
  552.    {
  553.       png_warning(png_ptr, "Invalid cHRM white point");
  554.       png_crc_finish(png_ptr, 24);
  555.       return;
  556.    }
  557.  
  558.    png_crc_read(png_ptr, buf, 4);
  559.    val = png_get_uint_32(buf);
  560.    red_x = (float)val / (float)100000.0;
  561.  
  562.    png_crc_read(png_ptr, buf, 4);
  563.    val = png_get_uint_32(buf);
  564.    red_y = (float)val / (float)100000.0;
  565.  
  566.    if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
  567.        red_x + red_y > 1.0)
  568.    {
  569.       png_warning(png_ptr, "Invalid cHRM red point");
  570.       png_crc_finish(png_ptr, 16);
  571.       return;
  572.    }
  573.  
  574.    png_crc_read(png_ptr, buf, 4);
  575.    val = png_get_uint_32(buf);
  576.    green_x = (float)val / (float)100000.0;
  577.  
  578.    png_crc_read(png_ptr, buf, 4);
  579.    val = png_get_uint_32(buf);
  580.    green_y = (float)val / (float)100000.0;
  581.  
  582.    if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
  583.        green_x + green_y > 1.0)
  584.    {
  585.       png_warning(png_ptr, "Invalid cHRM green point");
  586.       png_crc_finish(png_ptr, 8);
  587.       return;
  588.    }
  589.  
  590.    png_crc_read(png_ptr, buf, 4);
  591.    val = png_get_uint_32(buf);
  592.    blue_x = (float)val / (float)100000.0;
  593.  
  594.    png_crc_read(png_ptr, buf, 4);
  595.    val = png_get_uint_32(buf);
  596.    blue_y = (float)val / (float)100000.0;
  597.  
  598.    if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
  599.        blue_x + blue_y > 1.0)
  600.    {
  601.       png_warning(png_ptr, "Invalid cHRM blue point");
  602.       png_crc_finish(png_ptr, 0);
  603.       return;
  604.    }
  605.  
  606.    if (png_crc_finish(png_ptr, 0))
  607.       return;
  608.  
  609.    png_read_cHRM(png_ptr, info_ptr,
  610.       white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
  611. }
  612. #endif
  613.  
  614. #if defined(PNG_READ_tRNS_SUPPORTED)
  615. void
  616. png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  617. {
  618.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  619.       png_error(png_ptr, "Missing IHDR before tRNS");
  620.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  621.    {
  622.       png_warning(png_ptr, "Invalid tRNS after IDAT");
  623.       png_crc_finish(png_ptr, length);
  624.       return;
  625.    }
  626.    else if (info_ptr && info_ptr->valid & PNG_INFO_tRNS)
  627.    {
  628.       png_warning(png_ptr, "Duplcate tRNS chunk");
  629.       png_crc_finish(png_ptr, length);
  630.       return;
  631.    }
  632.  
  633.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  634.    {
  635.       if (!(png_ptr->mode & PNG_HAVE_PLTE))
  636.       {
  637.          /* Should be an error, but we can cope with it */
  638.          png_warning(png_ptr, "Missing PLTE before tRNS");
  639.       }
  640.       else if (length > png_ptr->num_palette)
  641.       {
  642.          png_warning(png_ptr, "Incorrect tRNS chunk length");
  643.          png_crc_finish(png_ptr, length);
  644.          return;
  645.       }
  646.  
  647.       png_ptr->trans = (png_bytep)png_malloc(png_ptr, length);
  648.       png_ptr->flags |= PNG_FLAG_FREE_TRANS;
  649.       png_crc_read(png_ptr, png_ptr->trans, length);
  650.       png_ptr->num_trans = (png_uint_16)length;
  651.    }
  652.    else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  653.    {
  654.       png_byte buf[6];
  655.  
  656.       if (length != 6)
  657.       {
  658.          png_warning(png_ptr, "Incorrect tRNS chunk length");
  659.          png_crc_finish(png_ptr, length);
  660.          return;
  661.       }
  662.  
  663.       png_crc_read(png_ptr, buf, length);
  664.       png_ptr->num_trans = 3;
  665.       png_ptr->trans_values.red = png_get_uint_16(buf);
  666.       png_ptr->trans_values.green = png_get_uint_16(buf + 2);
  667.       png_ptr->trans_values.blue = png_get_uint_16(buf + 4);
  668.    }
  669.    else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  670.    {
  671.       png_byte buf[6];
  672.  
  673.       if (length != 2)
  674.       {
  675.          png_warning(png_ptr, "Incorrect tRNS chunk length");
  676.          png_crc_finish(png_ptr, length);
  677.          return;
  678.       }
  679.  
  680.       png_crc_read(png_ptr, buf, 2);
  681.       png_ptr->num_trans = 1;
  682.       png_ptr->trans_values.gray = png_get_uint_16(buf);
  683.    }
  684.    else
  685.    {
  686.       png_warning(png_ptr, "tRNS chunk not allowed with alpha channel");
  687.       png_crc_finish(png_ptr, length);
  688.       return;
  689.    }
  690.  
  691.    if (png_crc_finish(png_ptr, 0))
  692.       return;
  693.  
  694.    png_read_tRNS(png_ptr, info_ptr, png_ptr->trans, png_ptr->num_trans,
  695.       &(png_ptr->trans_values));
  696. }
  697. #endif
  698.  
  699. #if defined(PNG_READ_bKGD_SUPPORTED)
  700. void
  701. png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  702. {
  703.    png_uint_32 truelen;
  704.    png_byte buf[6];
  705.  
  706.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  707.       png_error(png_ptr, "Missing IHDR before bKGD");
  708.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  709.    {
  710.       png_warning(png_ptr, "Invalid bKGD after IDAT");
  711.       png_crc_finish(png_ptr, length);
  712.       return;
  713.    }
  714.    else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  715.             !(png_ptr->mode & PNG_HAVE_PLTE))
  716.    {
  717.       png_warning(png_ptr, "Missing PLTE before bKGD");
  718.       png_crc_finish(png_ptr, length);
  719.       return;
  720.    }
  721.    else if (info_ptr && info_ptr->valid & PNG_INFO_bKGD)
  722.    {
  723.       png_warning(png_ptr, "Duplicate bKGD chunk");
  724.       png_crc_finish(png_ptr, length);
  725.       return;
  726.    }
  727.  
  728.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  729.       truelen = 1;
  730.    else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  731.       truelen = 6;
  732.    else
  733.       truelen = 2;
  734.  
  735.    if (length != truelen)
  736.    {
  737.       png_warning(png_ptr, "Incorrect bKGD chunk length");
  738.       png_crc_finish(png_ptr, length);
  739.       return;
  740.    }
  741.  
  742.    png_crc_read(png_ptr, buf, length);
  743.    if (png_crc_finish(png_ptr, 0))
  744.       return;
  745.  
  746.    /* We convert the index value into RGB components so that we can allow
  747.     * arbitrary RGB values for background when we have transparency, and
  748.     * so it is easy to determine the RGB values of the background color
  749.     * from the info_ptr struct. */
  750.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  751.    {
  752.       png_ptr->background.index = buf[0];
  753.       png_ptr->background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
  754.       png_ptr->background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
  755.       png_ptr->background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
  756.    }
  757.    else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR))
  758.    {
  759.       png_ptr->background.red =
  760.       png_ptr->background.green =
  761.       png_ptr->background.blue =
  762.       png_ptr->background.gray = png_get_uint_16(buf);
  763.    }
  764.    else
  765.    {
  766.       png_ptr->background.red = png_get_uint_16(buf);
  767.       png_ptr->background.green = png_get_uint_16(buf + 2);
  768.       png_ptr->background.blue = png_get_uint_16(buf + 4);
  769.    }
  770.  
  771.    png_read_bKGD(png_ptr, info_ptr, &(png_ptr->background));
  772. }
  773. #endif
  774.  
  775. #if defined(PNG_READ_hIST_SUPPORTED)
  776. void
  777. png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  778. {
  779.    int num, i;
  780.  
  781.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  782.       png_error(png_ptr, "Missing IHDR before hIST");
  783.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  784.    {
  785.       png_warning(png_ptr, "Invalid hIST after IDAT");
  786.       png_crc_finish(png_ptr, length);
  787.       return;
  788.    }
  789.    else if (!(png_ptr->mode & PNG_HAVE_PLTE))
  790.    {
  791.       png_warning(png_ptr, "Missing PLTE before hIST");
  792.       png_crc_finish(png_ptr, length);
  793.       return;
  794.    }
  795.    else if (info_ptr && info_ptr->valid & PNG_INFO_hIST)
  796.    {
  797.       png_warning(png_ptr, "Duplicate hIST chunk");
  798.       png_crc_finish(png_ptr, length);
  799.       return;
  800.    }
  801.  
  802.    if (length != 2 * png_ptr->num_palette)
  803.    {
  804.       png_warning(png_ptr, "Incorrect hIST chunk length");
  805.       png_crc_finish(png_ptr, length);
  806.       return;
  807.    }
  808.  
  809.    num = (int)length / 2;
  810.    png_ptr->hist = (png_uint_16p)png_malloc(png_ptr,
  811.       num * sizeof (png_uint_16));
  812.    png_ptr->flags |= PNG_FLAG_FREE_HIST;
  813.    for (i = 0; i < num; i++)
  814.    {
  815.       png_byte buf[2];
  816.  
  817.       png_crc_read(png_ptr, buf, 2);
  818.       png_ptr->hist[i] = png_get_uint_16(buf);
  819.    }
  820.  
  821.    if (png_crc_finish(png_ptr, 0))
  822.       return;
  823.  
  824.    png_read_hIST(png_ptr, info_ptr, png_ptr->hist);
  825. }
  826. #endif
  827.  
  828. #if defined(PNG_READ_pHYs_SUPPORTED)
  829. void
  830. png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  831. {
  832.    png_byte buf[9];
  833.    png_uint_32 res_x, res_y;
  834.    int unit_type;
  835.  
  836.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  837.       png_error(png_ptr, "Missing IHDR before pHYS");
  838.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  839.    {
  840.       png_warning(png_ptr, "Invalid pHYS after IDAT");
  841.       png_crc_finish(png_ptr, length);
  842.       return;
  843.    }
  844.    else if (info_ptr && info_ptr->valid & PNG_INFO_pHYs)
  845.    {
  846.       png_warning(png_ptr, "Duplicate pHYS chunk");
  847.       png_crc_finish(png_ptr, length);
  848.       return;
  849.    }
  850.  
  851.    if (length != 9)
  852.    {
  853.       png_warning(png_ptr, "Incorrect pHYs chunk length");
  854.       png_crc_finish(png_ptr, length);
  855.       return;
  856.    }
  857.  
  858.    png_crc_read(png_ptr, buf, 9);
  859.    if (png_crc_finish(png_ptr, 0))
  860.       return;
  861.  
  862.    res_x = png_get_uint_32(buf);
  863.    res_y = png_get_uint_32(buf + 4);
  864.    unit_type = buf[8];
  865.    png_read_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
  866. }
  867. #endif
  868.  
  869. #if defined(PNG_READ_oFFs_SUPPORTED)
  870. void
  871. png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  872. {
  873.    png_byte buf[9];
  874.    png_uint_32 offset_x, offset_y;
  875.    int unit_type;
  876.  
  877.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  878.       png_error(png_ptr, "Missing IHDR before oFFs");
  879.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  880.    {
  881.       png_warning(png_ptr, "Invalid oFFs after IDAT");
  882.       png_crc_finish(png_ptr, length);
  883.       return;
  884.    }
  885.    else if (info_ptr && info_ptr->valid & PNG_INFO_oFFs)
  886.    {
  887.       png_warning(png_ptr, "Duplicate oFFs chunk");
  888.       png_crc_finish(png_ptr, length);
  889.       return;
  890.    }
  891.  
  892.    if (length != 9)
  893.    {
  894.       png_warning(png_ptr, "Incorrect oFFs chunk length");
  895.       png_crc_finish(png_ptr, length);
  896.       return;
  897.    }
  898.  
  899.    png_crc_read(png_ptr, buf, 9);
  900.    if (png_crc_finish(png_ptr, 0))
  901.       return;
  902.  
  903.    offset_x = png_get_uint_32(buf);
  904.    offset_y = png_get_uint_32(buf + 4);
  905.    unit_type = buf[8];
  906.    png_read_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
  907. }
  908. #endif
  909.  
  910. #if defined(PNG_READ_tIME_SUPPORTED)
  911. void
  912. png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  913. {
  914.    png_byte buf[7];
  915.    png_time mod_time;
  916.  
  917.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  918.       png_error(png_ptr, "Out of place tIME chunk");
  919.    else if (info_ptr && info_ptr->valid & PNG_INFO_tIME)
  920.    {
  921.       png_warning(png_ptr, "Duplicate tIME chunk");
  922.       png_crc_finish(png_ptr, length);
  923.       return;
  924.    }
  925.  
  926.    if (png_ptr->mode & PNG_HAVE_IDAT)
  927.       png_ptr->mode |= PNG_AFTER_IDAT;
  928.  
  929.    if (length != 7)
  930.    {
  931.       png_warning(png_ptr, "Incorrect tIME chunk length");
  932.       png_crc_finish(png_ptr, length);
  933.       return;
  934.    }
  935.  
  936.    png_crc_read(png_ptr, buf, 7);
  937.    if (png_crc_finish(png_ptr, 0))
  938.       return;
  939.  
  940.    mod_time.second = buf[6];
  941.    mod_time.minute = buf[5];
  942.    mod_time.hour = buf[4];
  943.    mod_time.day = buf[3];
  944.    mod_time.month = buf[2];
  945.    mod_time.year = png_get_uint_16(buf);
  946.  
  947.    png_read_tIME(png_ptr, info_ptr, &mod_time);
  948. }
  949. #endif
  950.  
  951. #if defined(PNG_READ_tEXt_SUPPORTED)
  952. /* note: this does not correctly handle chunks that are > 64K */
  953. void
  954. png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  955. {
  956.    png_charp key;
  957.    png_charp text;
  958.  
  959.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  960.       png_error(png_ptr, "Missing IHDR before tEXt");
  961.  
  962.    if (png_ptr->mode & PNG_HAVE_IDAT)
  963.       png_ptr->mode |= PNG_AFTER_IDAT;
  964.  
  965.    key = (png_charp )png_malloc(png_ptr, length + 1);
  966.    png_crc_read(png_ptr, (png_bytep )key, length);
  967.    if (png_crc_finish(png_ptr, 0))
  968.    {
  969.       png_free(png_ptr, key);
  970.       return;
  971.    }
  972.  
  973.    key[(png_size_t)length] = '\0';
  974.  
  975.    for (text = key; *text; text++)
  976.       /* empty loop to check key length */ ;
  977.  
  978.    if (text != key + (png_size_t)length)
  979.       text++;
  980.  
  981.    png_read_tEXt(png_ptr, info_ptr, key, text, length - (text - key));
  982. }
  983. #endif
  984.  
  985. #if defined(PNG_READ_zTXt_SUPPORTED)
  986. /* note: this does not correctly handle chunks that are > 64K compressed
  987.    on those systems that can't malloc more than 64KB at a time. */
  988. void
  989. png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  990. {
  991.    static char msg[] = "Error decoding zTXt chunk";
  992.    png_charp key;
  993.    png_charp text;
  994.    png_uint_32 text_size, key_size;
  995.  
  996.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  997.       png_error(png_ptr, "Missing IHDR before zTXt");
  998.  
  999.    if (png_ptr->mode & PNG_HAVE_IDAT)
  1000.       png_ptr->mode |= PNG_AFTER_IDAT;
  1001.  
  1002.    key = png_malloc(png_ptr, length + 1);
  1003.    png_crc_read(png_ptr, (png_bytep )key, length);
  1004.    if (png_crc_finish(png_ptr, 0))
  1005.    {
  1006.       png_free(png_ptr, key);
  1007.       return;
  1008.    }
  1009.  
  1010.    key[(png_size_t)length] = '\0';
  1011.  
  1012.    for (text = key; *text; text++)
  1013.       /* empty loop */ ;
  1014.  
  1015.    /* zTXt can't have zero text */
  1016.    if (text == key + (png_size_t)length)
  1017.    {
  1018.       png_warning(png_ptr, "Zero length zTXt chunk");
  1019.       text_size = 0;
  1020.    }
  1021.    else if (*(++text)) /* check compression type byte */
  1022.    {
  1023.       png_warning(png_ptr, "Unknown zTXt compression type");
  1024.  
  1025.       /* Copy what we can of the error message into the text chunk */
  1026.       text_size = length - (text - key) - 1;
  1027.       text_size = sizeof(msg) > text_size ? text_size : sizeof(msg);
  1028.       png_memcpy(text, msg, (png_size_t)(text_size + 1));
  1029.    }
  1030.    else
  1031.    {
  1032.       text++;
  1033.  
  1034.       png_ptr->zstream.next_in = (png_bytep )text;
  1035.       png_ptr->zstream.avail_in = (uInt)(length - (text - key));
  1036.       png_ptr->zstream.next_out = png_ptr->zbuf;
  1037.       png_ptr->zstream.avail_out = (png_size_t)png_ptr->zbuf_size;
  1038.  
  1039.       key_size = text - key;
  1040.       text_size = 0;
  1041.       text = NULL;
  1042.  
  1043.       while (png_ptr->zstream.avail_in)
  1044.       {
  1045.          int ret;
  1046.  
  1047.          ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
  1048.          if (ret != Z_OK && ret != Z_STREAM_END)
  1049.          {
  1050.             if (png_ptr->zstream.msg)
  1051.                png_warning(png_ptr, png_ptr->zstream.msg);
  1052.             else
  1053.                png_warning(png_ptr, "zTXt decompression error");
  1054.             inflateReset(&png_ptr->zstream);
  1055.             png_ptr->zstream.avail_in = 0;
  1056.  
  1057.             if (!text)
  1058.             {
  1059.                text_size = key_size + sizeof(msg) + 1;
  1060.                text = (png_charp)png_malloc(png_ptr, text_size);
  1061.                png_memcpy(text, key, (png_size_t)key_size);
  1062.             }
  1063.  
  1064.             text[text_size - 1] = '\0';
  1065.  
  1066.             /* Copy what we can of the error message into the text chunk */
  1067.             text_size = length - (text - key) - 1;
  1068.             text_size = sizeof(msg) > text_size ? text_size : sizeof(msg);
  1069.             png_memcpy(text + key_size, msg, (png_size_t)(text_size + 1));
  1070.             break;
  1071.          }
  1072.          if (!png_ptr->zstream.avail_out || ret == Z_STREAM_END)
  1073.          {
  1074.             if (!text)
  1075.             {
  1076.                text = (png_charp)png_malloc(png_ptr,
  1077.                   png_ptr->zbuf_size - png_ptr->zstream.avail_out +
  1078.                      key_size + 1);
  1079.                png_memcpy(text + (png_size_t)key_size, png_ptr->zbuf,
  1080.                   (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out));
  1081.                png_memcpy(text, key, (png_size_t)key_size);
  1082.                text_size = key_size + (png_size_t)png_ptr->zbuf_size -
  1083.                   png_ptr->zstream.avail_out;
  1084.                *(text + (png_size_t)text_size) = '\0';
  1085.             }
  1086.             else
  1087.             {
  1088.                png_charp tmp;
  1089.  
  1090.                tmp = text;
  1091.                text = png_malloc(png_ptr, text_size +
  1092.                   png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1);
  1093.                png_memcpy(text, tmp, (png_size_t)text_size);
  1094.                png_free(png_ptr, tmp);
  1095.                png_memcpy(text + (png_size_t)text_size, png_ptr->zbuf,
  1096.                   (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out));
  1097.                text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
  1098.                *(text + (png_size_t)text_size) = '\0';
  1099.             }
  1100.             if (ret != Z_STREAM_END)
  1101.             {
  1102.                png_ptr->zstream.next_out = png_ptr->zbuf;
  1103.                png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  1104.             }
  1105.             else
  1106.             {
  1107.                break;
  1108.             }
  1109.          }
  1110.       }
  1111.  
  1112.       inflateReset(&png_ptr->zstream);
  1113.       png_ptr->zstream.avail_in = 0;
  1114.  
  1115.       png_free(png_ptr, key);
  1116.       key = text;
  1117.       text += (png_size_t)key_size;
  1118.       text_size -= key_size;
  1119.    }
  1120.  
  1121.    png_read_zTXt(png_ptr, info_ptr, key, text, text_size, 0);
  1122. }
  1123. #endif
  1124.  
  1125. /* This function is called when we haven't found a handler for a
  1126.    chunk.  If there isn't a problem with the chunk itself (ie bad
  1127.    chunk name or a critical chunk), the chunk is silently ignored. */
  1128. void
  1129. png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1130. {
  1131.    /* In the future we can have code here that calls user-supplied
  1132.     * callback functions for unknown chunks before they are ignored or
  1133.     * cause an error.
  1134.     */
  1135.    png_check_chunk_name(png_ptr, png_ptr->chunk_name);
  1136.  
  1137.    if (!(png_ptr->chunk_name[0] & 0x20))
  1138.    {
  1139.       char msg[40];
  1140.  
  1141.       sprintf(msg, "Unknown critical chunk %s", png_ptr->chunk_name);
  1142.       png_error(png_ptr, msg);
  1143.    }
  1144.  
  1145.    if (png_ptr->mode & PNG_HAVE_IDAT)
  1146.       png_ptr->mode |= PNG_AFTER_IDAT;
  1147.  
  1148.    png_crc_finish(png_ptr, length);
  1149. }
  1150.  
  1151. /* This function is called to verify that a chunk name is valid.
  1152.    This function can't have the "critical chunk check" incorporated
  1153.    into it, as in the future, we will need to be able to call user
  1154.    functions to handle unknown critical chunks after we check that
  1155.    the chunk name itself is valid. */
  1156. void
  1157. png_check_chunk_name(png_structp png_ptr, png_bytep chunk_name)
  1158. {
  1159.    if (chunk_name[0] < 41 || chunk_name[0] > 122  ||
  1160.        (chunk_name[0] > 90 && chunk_name[0] < 97) ||
  1161.        chunk_name[1] < 41 || chunk_name[1] > 122  ||
  1162.        (chunk_name[1] > 90 && chunk_name[1] < 97) ||
  1163.        chunk_name[2] < 41 || chunk_name[2] > 122  ||
  1164.        (chunk_name[2] > 90 && chunk_name[2] < 97) ||
  1165.        chunk_name[3] < 41 || chunk_name[3] > 122  ||
  1166.        (chunk_name[3] > 90 && chunk_name[3] < 97))
  1167.    {
  1168.       char msg[45];
  1169.  
  1170.       sprintf(msg, "Invalid chunk type 0x%02X 0x%02X 0x%02X 0x%02X",
  1171.          chunk_name[0], chunk_name[1], chunk_name[2], chunk_name[3]);
  1172.       png_error(png_ptr, msg);
  1173.    }
  1174. }
  1175.  
  1176. /* Combines the row recently read in with the previous row.
  1177.    This routine takes care of alpha and transparency if requested.
  1178.    This routine also handles the two methods of progressive display
  1179.    of interlaced images, depending on the mask value.
  1180.    The mask value describes which pixels are to be combined with
  1181.    the row.  The pattern always repeats every 8 pixels, so just 8
  1182.    bits are needed.  A one indicates the pixels is to be combined,
  1183.    a zero indicates the pixel is to be skipped.  This is in addition
  1184.    to any alpha or transparency value associated with the pixel.  If
  1185.    you want all pixels to be combined, pass 0xff (255) in mask.  */
  1186. void
  1187. png_combine_row(png_structp png_ptr, png_bytep row,
  1188.    int mask)
  1189. {
  1190.    if (mask == 0xff)
  1191.    {
  1192.       png_memcpy(row, png_ptr->row_buf + 1,
  1193.          (png_size_t)((png_ptr->width *
  1194.          png_ptr->row_info.pixel_depth + 7) >> 3));
  1195.    }
  1196.    else
  1197.    {
  1198.       switch (png_ptr->row_info.pixel_depth)
  1199.       {
  1200.          case 1:
  1201.          {
  1202.             png_bytep sp;
  1203.             png_bytep dp;
  1204.             int m;
  1205.             int shift;
  1206.             png_uint_32 i;
  1207.             int value;
  1208.  
  1209.             sp = png_ptr->row_buf + 1;
  1210.             dp = row;
  1211.             shift = 7;
  1212.             m = 0x80;
  1213.             for (i = 0; i < png_ptr->width; i++)
  1214.             {
  1215.                if (m & mask)
  1216.                {
  1217.                   value = (*sp >> shift) & 0x1;
  1218.                   *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
  1219.                   *dp |= (png_byte)(value << shift);
  1220.                }
  1221.  
  1222.                if (shift == 0)
  1223.                {
  1224.                   shift = 7;
  1225.                   sp++;
  1226.                   dp++;
  1227.                }
  1228.                else
  1229.                   shift--;
  1230.  
  1231.                if (m == 1)
  1232.                   m = 0x80;
  1233.                else
  1234.                   m >>= 1;
  1235.             }
  1236.             break;
  1237.          }
  1238.          case 2:
  1239.          {
  1240.             png_bytep sp;
  1241.             png_bytep dp;
  1242.             int m;
  1243.             int shift;
  1244.             png_uint_32 i;
  1245.             int value;
  1246.  
  1247.             sp = png_ptr->row_buf + 1;
  1248.             dp = row;
  1249.             shift = 6;
  1250.             m = 0x80;
  1251.             for (i = 0; i < png_ptr->width; i++)
  1252.             {
  1253.                if (m & mask)
  1254.                {
  1255.                   value = (*sp >> shift) & 0x3;
  1256.                   *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
  1257.                   *dp |= (png_byte)(value << shift);
  1258.                }
  1259.  
  1260.                if (shift == 0)
  1261.                {
  1262.                   shift = 6;
  1263.                   sp++;
  1264.                   dp++;
  1265.                }
  1266.                else
  1267.                   shift -= 2;
  1268.                if (m == 1)
  1269.                   m = 0x80;
  1270.                else
  1271.                   m >>= 1;
  1272.             }
  1273.             break;
  1274.          }
  1275.          case 4:
  1276.          {
  1277.             png_bytep sp;
  1278.             png_bytep dp;
  1279.             int m;
  1280.             int shift;
  1281.             png_uint_32 i;
  1282.             int value;
  1283.  
  1284.             sp = png_ptr->row_buf + 1;
  1285.             dp = row;
  1286.             shift = 4;
  1287.             m = 0x80;
  1288.             for (i = 0; i < png_ptr->width; i++)
  1289.             {
  1290.                if (m & mask)
  1291.                {
  1292.                   value = (*sp >> shift) & 0xf;
  1293.                   *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
  1294.                   *dp |= (png_byte)(value << shift);
  1295.                }
  1296.  
  1297.                if (shift == 0)
  1298.                {
  1299.                   shift = 4;
  1300.                   sp++;
  1301.                   dp++;
  1302.                }
  1303.                else
  1304.                   shift -= 4;
  1305.                if (m == 1)
  1306.                   m = 0x80;
  1307.                else
  1308.                   m >>= 1;
  1309.             }
  1310.             break;
  1311.          }
  1312.          default:
  1313.          {
  1314.             png_bytep sp;
  1315.             png_bytep dp;
  1316.             png_uint_32 i, pixel_bytes;
  1317.             png_byte m;
  1318.  
  1319.             pixel_bytes = (png_ptr->row_info.pixel_depth >> 3);
  1320.  
  1321.             sp = png_ptr->row_buf + 1;
  1322.             dp = row;
  1323.             m = 0x80;
  1324.             for (i = 0; i < png_ptr->width; i++)
  1325.             {
  1326.                if (m & mask)
  1327.                {
  1328.                   png_memcpy(dp, sp, pixel_bytes);
  1329.                }
  1330.  
  1331.                sp += pixel_bytes;
  1332.                dp += pixel_bytes;
  1333.  
  1334.                if (m == 1)
  1335.                   m = 0x80;
  1336.                else
  1337.                   m >>= 1;
  1338.             }
  1339.             break;
  1340.          }
  1341.       }
  1342.    }
  1343. }
  1344.  
  1345. #if defined(PNG_READ_INTERLACING_SUPPORTED)
  1346. void
  1347. png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass)
  1348. {
  1349.    if (row && row_info)
  1350.    {
  1351.       png_uint_32 final_width;
  1352.  
  1353.       final_width = row_info->width * png_pass_inc[pass];
  1354.  
  1355.       switch (row_info->pixel_depth)
  1356.       {
  1357.          case 1:
  1358.          {
  1359.             png_bytep sp, dp;
  1360.             int sshift, dshift;
  1361.             png_byte v;
  1362.             png_uint_32 i;
  1363.             int j;
  1364.  
  1365.             sp = row + (png_size_t)((row_info->width - 1) >> 3);
  1366.             sshift = 7 - (int)((row_info->width + 7) & 7);
  1367.             dp = row + (png_size_t)((final_width - 1) >> 3);
  1368.             dshift = 7 - (int)((final_width + 7) & 7);
  1369.             for (i = row_info->width; i; i--)
  1370.             {
  1371.                v = (png_byte)((*sp >> sshift) & 0x1);
  1372.                for (j = 0; j < png_pass_inc[pass]; j++)
  1373.                {
  1374.                   *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
  1375.                   *dp |= (png_byte)(v << dshift);
  1376.                   if (dshift == 7)
  1377.                   {
  1378.                      dshift = 0;
  1379.                      dp--;
  1380.                   }
  1381.                   else
  1382.                      dshift++;
  1383.                }
  1384.                if (sshift == 7)
  1385.                {
  1386.                   sshift = 0;
  1387.                   sp--;
  1388.                }
  1389.                else
  1390.                   sshift++;
  1391.             }
  1392.             break;
  1393.          }
  1394.          case 2:
  1395.          {
  1396.             png_bytep sp, dp;
  1397.             int sshift, dshift;
  1398.             png_byte v;
  1399.             png_uint_32 i, j;
  1400.  
  1401.             sp = row + (png_size_t)((row_info->width - 1) >> 2);
  1402.             sshift = (png_size_t)((3 - ((row_info->width + 3) & 3)) << 1);
  1403.             dp = row + (png_size_t)((final_width - 1) >> 2);
  1404.             dshift = (png_size_t)((3 - ((final_width + 3) & 3)) << 1);
  1405.             for (i = row_info->width; i; i--)
  1406.             {
  1407.                v = (png_byte)((*sp >> sshift) & 0x3);
  1408.                for (j = 0; j < png_pass_inc[pass]; j++)
  1409.                {
  1410.                   *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
  1411.                   *dp |= (png_byte)(v << dshift);
  1412.                   if (dshift == 6)
  1413.                   {
  1414.                      dshift = 0;
  1415.                      dp--;
  1416.                   }
  1417.                   else
  1418.                      dshift += 2;
  1419.                }
  1420.                if (sshift == 6)
  1421.                {
  1422.                   sshift = 0;
  1423.                   sp--;
  1424.                }
  1425.                else
  1426.                   sshift += 2;
  1427.             }
  1428.             break;
  1429.          }
  1430.          case 4:
  1431.          {
  1432.             png_bytep sp, dp;
  1433.             int sshift, dshift;
  1434.             png_byte v;
  1435.             png_uint_32 i;
  1436.             int j;
  1437.  
  1438.             sp = row + (png_size_t)((row_info->width - 1) >> 1);
  1439.             sshift = (png_size_t)((1 - ((row_info->width + 1) & 1)) << 2);
  1440.             dp = row + (png_size_t)((final_width - 1) >> 1);
  1441.             dshift = (png_size_t)((1 - ((final_width + 1) & 1)) << 2);
  1442.             for (i = row_info->width; i; i--)
  1443.             {
  1444.                v = (png_byte)((*sp >> sshift) & 0xf);
  1445.                for (j = 0; j < png_pass_inc[pass]; j++)
  1446.                {
  1447.                   *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);
  1448.                   *dp |= (png_byte)(v << dshift);
  1449.                   if (dshift == 4)
  1450.                   {
  1451.                      dshift = 0;
  1452.                      dp--;
  1453.                   }
  1454.                   else
  1455.                      dshift = 4;
  1456.                }
  1457.                if (sshift == 4)
  1458.                {
  1459.                   sshift = 0;
  1460.                   sp--;
  1461.                }
  1462.                else
  1463.                   sshift = 4;
  1464.             }
  1465.             break;
  1466.          }
  1467.          default:
  1468.          {
  1469.             png_bytep sp, dp;
  1470.             png_byte v[8];
  1471.             png_uint_32 i, pixel_bytes;
  1472.             int j;
  1473.  
  1474.             pixel_bytes = (row_info->pixel_depth >> 3);
  1475.  
  1476.             sp = row + (png_size_t)((row_info->width - 1) * pixel_bytes);
  1477.             dp = row + (png_size_t)((final_width - 1) * pixel_bytes);
  1478.             for (i = row_info->width; i; i--)
  1479.             {
  1480.                png_memcpy(v, sp, pixel_bytes);
  1481.                for (j = 0; j < png_pass_inc[pass]; j++)
  1482.                {
  1483.                   png_memcpy(dp, v, pixel_bytes);
  1484.                   dp -= pixel_bytes;
  1485.                }
  1486.                sp -= pixel_bytes;
  1487.             }
  1488.             break;
  1489.          }
  1490.       }
  1491.       row_info->width = final_width;
  1492.       row_info->rowbytes = ((final_width *
  1493.          (png_uint_32)row_info->pixel_depth + 7) >> 3);
  1494.    }
  1495. }
  1496. #endif
  1497.  
  1498. void
  1499. png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row,
  1500.    png_bytep prev_row, int filter)
  1501. {
  1502.    switch (filter)
  1503.    {
  1504.       case 0:
  1505.          break;
  1506.       case 1:
  1507.       {
  1508.          png_uint_32 i;
  1509.          int bpp;
  1510.          png_bytep rp;
  1511.          png_bytep lp;
  1512.  
  1513.          bpp = (row_info->pixel_depth + 7) / 8;
  1514.          for (i = (png_uint_32)bpp, rp = row + bpp, lp = row;
  1515.             i < row_info->rowbytes; i++, rp++, lp++)
  1516.          {
  1517.             *rp = (png_byte)(((int)(*rp) + (int)(*lp)) & 0xff);
  1518.          }
  1519.          break;
  1520.       }
  1521.       case 2:
  1522.       {
  1523.          png_uint_32 i;
  1524.          png_bytep rp;
  1525.          png_bytep pp;
  1526.  
  1527.          for (i = 0, rp = row, pp = prev_row;
  1528.             i < row_info->rowbytes; i++, rp++, pp++)
  1529.          {
  1530.             *rp = (png_byte)(((int)(*rp) + (int)(*pp)) & 0xff);
  1531.          }
  1532.          break;
  1533.       }
  1534.       case 3:
  1535.       {
  1536.          png_uint_32 i;
  1537.          int bpp;
  1538.          png_bytep rp;
  1539.          png_bytep pp;
  1540.          png_bytep lp;
  1541.  
  1542.          bpp = (row_info->pixel_depth + 7) / 8;
  1543.          for (i = 0, rp = row, pp = prev_row;
  1544.             i < (png_uint_32)bpp; i++, rp++, pp++)
  1545.          {
  1546.             *rp = (png_byte)(((int)(*rp) +
  1547.                ((int)(*pp) / 2)) & 0xff);
  1548.          }
  1549.          for (lp = row; i < row_info->rowbytes; i++, rp++, lp++, pp++)
  1550.          {
  1551.             *rp = (png_byte)(((int)(*rp) +
  1552.                (int)(*pp + *lp) / 2) & 0xff);
  1553.          }
  1554.          break;
  1555.       }
  1556.       case 4:
  1557.       {
  1558.          int bpp;
  1559.          png_uint_32 i;
  1560.          png_bytep rp;
  1561.          png_bytep pp;
  1562.          png_bytep lp;
  1563.          png_bytep cp;
  1564.  
  1565.          bpp = (row_info->pixel_depth + 7) / 8;
  1566.          for (i = 0, rp = row, pp = prev_row,
  1567.             lp = row - bpp, cp = prev_row - bpp;
  1568.             i < row_info->rowbytes; i++, rp++, pp++, lp++, cp++)
  1569.          {
  1570.             int a, b, c, pa, pb, pc, p;
  1571.  
  1572.             b = *pp;
  1573.             if (i >= (png_uint_32)bpp)
  1574.             {
  1575.                c = *cp;
  1576.                a = *lp;
  1577.             }
  1578.             else
  1579.             {
  1580.                a = c = 0;
  1581.             }
  1582.             p = a + b - c;
  1583.             pa = abs(p - a);
  1584.             pb = abs(p - b);
  1585.             pc = abs(p - c);
  1586.  
  1587.             if (pa <= pb && pa <= pc)
  1588.                p = a;
  1589.             else if (pb <= pc)
  1590.                p = b;
  1591.             else
  1592.                p = c;
  1593.  
  1594.             *rp = (png_byte)(((int)(*rp) + p) & 0xff);
  1595.          }
  1596.          break;
  1597.       }
  1598.       default:
  1599.          png_error(png_ptr, "Bad adaptive filter type");
  1600.          break;
  1601.    }
  1602. }
  1603.  
  1604. void
  1605. png_read_finish_row(png_structp png_ptr)
  1606. {
  1607.    png_ptr->row_number++;
  1608.    if (png_ptr->row_number < png_ptr->num_rows)
  1609.       return;
  1610.  
  1611.    if (png_ptr->interlaced)
  1612.    {
  1613.       png_ptr->row_number = 0;
  1614.       png_memset(png_ptr->prev_row, 0, (png_size_t)png_ptr->rowbytes + 1);
  1615.       do
  1616.       {
  1617.          png_ptr->pass++;
  1618.          if (png_ptr->pass >= 7)
  1619.             break;
  1620.          png_ptr->iwidth = (png_ptr->width +
  1621.             png_pass_inc[png_ptr->pass] - 1 -
  1622.             png_pass_start[png_ptr->pass]) /
  1623.             png_pass_inc[png_ptr->pass];
  1624.          png_ptr->irowbytes = ((png_ptr->iwidth *
  1625.             png_ptr->pixel_depth + 7) >> 3) + 1;
  1626.          if (!(png_ptr->transformations & PNG_INTERLACE))
  1627.          {
  1628.             png_ptr->num_rows = (png_ptr->height +
  1629.                png_pass_yinc[png_ptr->pass] - 1 -
  1630.                png_pass_ystart[png_ptr->pass]) /
  1631.                png_pass_yinc[png_ptr->pass];
  1632.             if (!(png_ptr->num_rows))
  1633.                continue;
  1634.          }
  1635.          if (png_ptr->transformations & PNG_INTERLACE)
  1636.             break;
  1637.       } while (png_ptr->iwidth == 0);
  1638.  
  1639.       if (png_ptr->pass < 7)
  1640.          return;
  1641.    }
  1642.  
  1643.    if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
  1644.    {
  1645.       char extra;
  1646.       int ret;
  1647.  
  1648.       png_ptr->zstream.next_out = (Byte *)&extra;
  1649.       png_ptr->zstream.avail_out = (uInt)1;
  1650.       do
  1651.       {
  1652.          if (!(png_ptr->zstream.avail_in))
  1653.          {
  1654.             while (!png_ptr->idat_size)
  1655.             {
  1656.                png_byte chunk_length[4];
  1657.  
  1658.                png_crc_finish(png_ptr, 0);
  1659.  
  1660.                png_read_data(png_ptr, chunk_length, 4);
  1661.                png_ptr->idat_size = png_get_uint_32(chunk_length);
  1662.  
  1663.                png_reset_crc(png_ptr);
  1664.                png_crc_read(png_ptr, png_ptr->chunk_name, 4);
  1665.                if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  1666.                   png_error(png_ptr, "Not enough image data");
  1667.  
  1668.             }
  1669.             png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
  1670.             png_ptr->zstream.next_in = png_ptr->zbuf;
  1671.             if (png_ptr->zbuf_size > png_ptr->idat_size)
  1672.                png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
  1673.             png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in);
  1674.             png_ptr->idat_size -= png_ptr->zstream.avail_in;
  1675.          }
  1676.          ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
  1677.          if (ret == Z_STREAM_END)
  1678.          {
  1679.             if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in ||
  1680.                png_ptr->idat_size)
  1681.                png_error(png_ptr, "Extra compressed data");
  1682.             png_ptr->mode |= PNG_AFTER_IDAT;
  1683.             png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  1684.             break;
  1685.          }
  1686.          if (ret != Z_OK)
  1687.             png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
  1688.                       "Decompression Error");
  1689.  
  1690.          if (!(png_ptr->zstream.avail_out))
  1691.             png_error(png_ptr, "Extra compressed data");
  1692.  
  1693.       } while (1);
  1694.       png_ptr->zstream.avail_out = 0;
  1695.    }
  1696.  
  1697.    if (png_ptr->idat_size || png_ptr->zstream.avail_in)
  1698.       png_error(png_ptr, "Extra compression data");
  1699.  
  1700.    inflateReset(&png_ptr->zstream);
  1701.  
  1702.    png_ptr->mode |= PNG_AFTER_IDAT;
  1703. }
  1704.  
  1705. void
  1706. png_read_start_row(png_structp png_ptr)
  1707. {
  1708.    int max_pixel_depth;
  1709.    png_uint_32 rowbytes;
  1710.  
  1711.    png_ptr->zstream.avail_in = 0;
  1712.    png_init_read_transformations(png_ptr);
  1713.    if (png_ptr->interlaced)
  1714.    {
  1715.       if (!(png_ptr->transformations & PNG_INTERLACE))
  1716.          png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
  1717.             png_pass_ystart[0]) / png_pass_yinc[0];
  1718.       else
  1719.          png_ptr->num_rows = png_ptr->height;
  1720.  
  1721.       png_ptr->iwidth = (png_ptr->width +
  1722.          png_pass_inc[png_ptr->pass] - 1 -
  1723.          png_pass_start[png_ptr->pass]) /
  1724.          png_pass_inc[png_ptr->pass];
  1725.       png_ptr->irowbytes = ((png_ptr->iwidth *
  1726.          png_ptr->pixel_depth + 7) >> 3) + 1;
  1727.    }
  1728.    else
  1729.    {
  1730.       png_ptr->num_rows = png_ptr->height;
  1731.       png_ptr->iwidth = png_ptr->width;
  1732.       png_ptr->irowbytes = png_ptr->rowbytes + 1;
  1733.    }
  1734.  
  1735.    max_pixel_depth = png_ptr->pixel_depth;
  1736.  
  1737. #if defined(PNG_READ_PACK_SUPPORTED)
  1738.    if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
  1739.       max_pixel_depth = 8;
  1740. #endif
  1741.  
  1742. #if defined(PNG_READ_EXPAND_SUPPORTED)
  1743.    if (png_ptr->transformations & PNG_EXPAND)
  1744.    {
  1745.       if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1746.       {
  1747.          if (png_ptr->num_trans)
  1748.             max_pixel_depth = 32;
  1749.          else
  1750.             max_pixel_depth = 24;
  1751.       }
  1752.       else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  1753.       {
  1754.          if (max_pixel_depth < 8)
  1755.             max_pixel_depth = 8;
  1756.          if (png_ptr->num_trans)
  1757.             max_pixel_depth *= 2;
  1758.       }
  1759.       else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  1760.       {
  1761.          if (png_ptr->num_trans)
  1762.          {
  1763.             max_pixel_depth *= 4;
  1764.             max_pixel_depth /= 3;
  1765.          }
  1766.       }
  1767.    }
  1768. #endif
  1769.  
  1770. #if defined(PNG_READ_FILLER_SUPPORTED)
  1771.    if (png_ptr->transformations & (PNG_FILLER))
  1772.    {
  1773.       if (max_pixel_depth < 32)
  1774.          max_pixel_depth = 32;
  1775.    }
  1776. #endif
  1777.  
  1778. #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
  1779.    if (png_ptr->transformations & PNG_GRAY_TO_RGB)
  1780.    {
  1781.       if ((png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
  1782.          png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  1783.       {
  1784.          if (max_pixel_depth <= 16)
  1785.             max_pixel_depth = 32;
  1786.          else if (max_pixel_depth <= 32)
  1787.             max_pixel_depth = 64;
  1788.       }
  1789.       else
  1790.       {
  1791.          if (max_pixel_depth <= 8)
  1792.             max_pixel_depth = 24;
  1793.          else if (max_pixel_depth <= 16)
  1794.             max_pixel_depth = 48;
  1795.       }
  1796.    }
  1797. #endif
  1798.  
  1799.    /* align the width on the next larger 8 pixels.  Mainly used
  1800.       for interlacing */
  1801.    rowbytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
  1802.    /* calculate the maximum bytes needed, adding a byte and a pixel
  1803.       for safety sake */
  1804.    rowbytes = ((rowbytes * (png_uint_32)max_pixel_depth + 7) >> 3) +
  1805.       1 + ((max_pixel_depth + 7) >> 3);
  1806. #ifdef PNG_MAX_MALLOC_64K
  1807.    if (rowbytes > 65536L)
  1808.       png_error(png_ptr, "This image requires a row greater than 64KB");
  1809. #endif
  1810.    png_ptr->row_buf = (png_bytep )png_malloc(png_ptr, rowbytes);
  1811.  
  1812. #ifdef PNG_MAX_MALLOC_64K
  1813.    if (png_ptr->rowbytes + 1 > 65536L)
  1814.       png_error(png_ptr, "This image requires a row greater than 64KB");
  1815. #endif
  1816.    png_ptr->prev_row = png_malloc(png_ptr, png_ptr->rowbytes + 1);
  1817.  
  1818.    png_memset(png_ptr->prev_row, 0, (png_size_t)png_ptr->rowbytes + 1);
  1819.  
  1820.    png_ptr->flags |= PNG_FLAG_ROW_INIT;
  1821. }
  1822.